package com.maxatime.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.maxatime.common.constant.BusinessDataConstants;
import com.maxatime.dao.MenuMapper;
import com.maxatime.pojo.dto.MenuDTO;
import com.maxatime.pojo.entity.Menu;
import com.maxatime.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService{
	
	// 日志
	Logger log = LogManager.getLogger();
	
	@Autowired
	MenuMapper menuMapper;

	@Override
	public List<MenuDTO> listMenu() {
		// TODO Auto-generated method stub
		
		List<MenuDTO> listMenu = new ArrayList<>();
		String parentId = BusinessDataConstants.MENU_ROOT_PARENT_ID;
		Menu menu = new Menu();
		menu.setParentId(parentId);
		menu.setIsused(BusinessDataConstants.MENU_ISUSED_0);
		menu.setDeleteFlag(BusinessDataConstants.DELETE_FLAG_STRING_0);
		List<Menu> listData = menuMapper.listData(menu);
		for(int i = 0; i < listData.size(); i++ ) {
			Menu menuRet = listData.get(i);
			MenuDTO menuDTO = new MenuDTO();
			BeanUtils.copyProperties(menuRet, menuDTO);
			String sParentId = menuRet.getId();
			List<Menu> listMenuData = listMenuData(sParentId);
			List<MenuDTO> listMenuDataDTO = new ArrayList<>();
			for(int j = 0; j < listMenuData.size(); j++) {
				Menu subMenuRet = listMenuData.get(j);
				MenuDTO subMenuDTO = new MenuDTO();
				BeanUtils.copyProperties(subMenuRet, subMenuDTO);
				listMenuDataDTO.add(subMenuDTO);
			}

			menuDTO.setChildren(listMenuDataDTO);
			listMenu.add(menuDTO);
		}
		return listMenu;
	}
	
	// 递归查询菜单
		private List<Menu> listMenuData(String parentId) {
			StringUtils.trimToEmpty(parentId);
			// 菜单查询参数
			Menu menu = new Menu();
			menu.setParentId(parentId);
			menu.setIsused(BusinessDataConstants.MENU_ISUSED_0);
			List<Menu> listData = menuMapper.listData(menu);
			return listData;
		}

		@Override
		public List<MenuDTO> listMenuRecursive() {
			// TODO Auto-generated method stub
			List<MenuDTO > listMenu = new ArrayList<>();
			String parentId = BusinessDataConstants.MENU_ROOT_PARENT_ID;
			Menu menu = new Menu();
			menu.setParentId(parentId);
			menu.setIsused(BusinessDataConstants.MENU_ISUSED_0);
			List<Menu> listData = menuMapper.listData(menu);
			for(int i = 0; i < listData.size(); i ++) {
				MenuDTO menuDTO = new MenuDTO();
				menuDTO = menuRecursive(listData.get(i));
				listMenu.add(menuDTO);
			}
			return listMenu;
		}
		private MenuDTO menuRecursive(Menu menu) {
			List<MenuDTO> listMenu = new ArrayList<MenuDTO>();
			
			MenuDTO menuDTO = new MenuDTO();
			BeanUtils.copyProperties(menu, menuDTO);

			Menu menuReq = new Menu();
			String parentId = menu.getId();
			menuReq.setParentId(parentId);
			menuReq.setIsused(BusinessDataConstants.MENU_ISUSED_0);
			menuReq.setDeleteFlag(BusinessDataConstants.DELETE_FLAG_STRING_0);
			List<Menu> listData = menuMapper.listData(menuReq);

			for(int i = 0; i < listData.size(); i++) {
				
				// List<MenuDTO> listMenuSub = new ArrayList<MenuDTO>();
				MenuDTO MenuDTO = new MenuDTO();
				MenuDTO = menuRecursive(listData.get(i));
				listMenu.add(MenuDTO);
			}
			menuDTO.setChildren(listMenu);
			return menuDTO;
		}


}
